package app.framework.freemarker.model;

import freemarker.template.TemplateModelException;
import jasmine.framework.common.util.CheckUtil;
import jasmine.framework.common.util.I18nUtil;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用于引用第三方库的本地化脚本。
 * </p>
 *
 * @author mh.z
 */
public class LibLangMethodModel extends AbstractResourceMethodModel {
    private static final String JS_SUFFIX = ".js";
    private static final String MIN_JS_SUFFIX = ".min.js";

    public LibLangMethodModel() {
        super();
    }

    public LibLangMethodModel(Map<String, String> mappingUrls) {
        super(mappingUrls);
    }

    @Override
    public Object exec(List args) throws TemplateModelException {
        CheckUtil.notNull(args, "args null");

        if (args.size() < 1) {
            return "";
        }

        String originalUrl = (String) args.get(0);
        String mappedUrl = getMappedUrl(originalUrl);
        String langCode = I18nUtil.getLanguage();

        String oldSuffix = mappedUrl.endsWith(MIN_JS_SUFFIX)
                ? MIN_JS_SUFFIX : JS_SUFFIX;
        String newSuffix = "-" + langCode + oldSuffix;
        // URL 后缀加上当前的语言代码
        String resultUrl = mappedUrl.replace(oldSuffix, newSuffix);

        return resultUrl;
    }

}
